package com.aliyun.oss.service.impl;

import com.aliyun.oss.mapper.LoginMapper;
import com.aliyun.oss.pojo.DTO.UserDTO;
import com.aliyun.oss.pojo.Entity.User;
import com.aliyun.oss.service.LoginService;
import com.aliyun.oss.utils.JwtUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class LoginServiceImpl extends ServiceImpl<LoginMapper,User> implements LoginService {
    @Override
    public String userLogin(UserDTO userDTO) {
//        使用mybasticplus根据用户名和密码查询用户，返回id
        User user = getOne(new QueryWrapper<User>().eq("username", userDTO.getUsername()).eq("password", userDTO.getPassword()));
        if (user != null) {
            String Id = user.getId().toString();
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("id",Id);
            String token = JwtUtils.createToken(hashMap);
            return token;
        }
        return null;
    }

    @Override
    public UserDTO selectUserByUsernameAndPassword(UserDTO userDTO) {
//        使用mybasticplus根据用户名和密码查询用户，返回User实例
        User user = getOne(new QueryWrapper<User>().eq("username", userDTO.getUsername()).eq("password", userDTO.getPassword()));
        UserDTO userDTO1 = new UserDTO();
        userDTO1.setUsername(user.getUsername());
        userDTO1.setPassword(user.getPassword());
        userDTO1.setStatus(user.getStatus());
        return userDTO1;
    }
}
